package com.mdd.admin.service.way.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.google.common.collect.Maps;
import com.mdd.admin.service.way.IDevPayWayService;
import com.mdd.admin.validate.way.DevPayWayParam;
import com.mdd.admin.vo.way.DevPayWayListVo;
import com.mdd.common.entity.way.DevPayWay;
import com.mdd.common.enums.ClientEnum;
import com.mdd.common.enums.OrderPlayTypeEnum;
import com.mdd.common.enums.StatusEnum;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.way.DevPayWayMapper;
import com.mdd.common.utils.UrlUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 支付方式实现类
 */
@Service
public class DevPayWayServiceImpl extends ServiceImpl<DevPayWayMapper, DevPayWay> implements IDevPayWayService {

    @Resource
    DevPayWayMapper devPayWayMapper;

    /**
     * 支付方式列表
     *
     * @return PageResult<DevPayWayListVo>
     */
    @Override
    public Map<Long, List<DevPayWayListVo>> listPage() {
        Map<Integer, String> map = OrderPlayTypeEnum.getMap();
        Map<Integer, String> clientMap = ClientEnum.getMap();
        Map<Long, List<DevPayWayListVo>> devPayWayListVoMap = Maps.newHashMap();

        MPJQueryWrapper<DevPayWay> mpjQueryWrapper = new MPJQueryWrapper<DevPayWay>()
                .selectAll(DevPayWay.class)
                .select("pay.name as payName, pay.image as payImage, pay.pay_way as payWay")
                .innerJoin("la_dev_pay pay ON pay.id=t.pay_id")
                .orderByDesc(Arrays.asList("t.id"));
        List<DevPayWayListVo> list = devPayWayMapper.selectJoinList(DevPayWayListVo.class, mpjQueryWrapper);

        List<DevPayWayListVo> minprogram = new LinkedList<>();
        List<DevPayWayListVo> officialAccounts = new LinkedList<>();
        List<DevPayWayListVo> h5 = new LinkedList<>();
        List<DevPayWayListVo> other = new LinkedList<>();

        //场景:1-微信小程序;2-微信公众号;3-H5;4-PC商城;5-APP;
        list.forEach(item -> {
            item.setPayWayDesc(map.get(item.getPayId()));
            item.setSceneDesc(clientMap.get(item.getScene()));
            item.setPayImage(UrlUtil.toAbsoluteUrl(item.getPayImage()));
            Integer scene = item.getScene();
            switch (scene) {
                case 1:
                    minprogram.add(item);
                    devPayWayListVoMap.put(1L, minprogram);
                    break;
                case 2:
                    officialAccounts.add(item);
                    devPayWayListVoMap.put(2L, officialAccounts);
                    break;
                case 3:
                    h5.add(item);
                    devPayWayListVoMap.put(3L, h5);
                    break;
                default:
                    other.add(item);
                    devPayWayListVoMap.put(item.getId(), other);
                    break;
            }
        });
        return devPayWayListVoMap;
    }


    /**
     * 支付方式编辑
     *
     * @param devPayWayParam 参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(List<DevPayWayParam> devPayWayParam) {
        for (DevPayWayParam payWayParam : devPayWayParam) {
            DevPayWay model = devPayWayMapper.selectOne(
                    new QueryWrapper<DevPayWay>()
                            .eq("id", payWayParam.getId())
                            .last("limit 1"));

            Assert.notNull(model, "数据不存在!");
            model.setId(payWayParam.getId());
            model.setScene(payWayParam.getScene());
            model.setPayId(payWayParam.getPayId());
            model.setIsDefault(payWayParam.getIsDefault());
            model.setStatus(payWayParam.getStatus());
            devPayWayMapper.updateById(model);
        }
    }

}
